package tuanda.soict.firstsun.AbilityPackage;

import java.util.ArrayList;
import java.util.Random;

import tuanda.soict.firstsun.EffectPackage.Attack_Effect;
import tuanda.soict.firstsun.LogicPackage.Army;
import tuanda.soict.firstsun.LogicPackage.BattleMap;
import tuanda.soict.firstsun.LogicPackage.Coordinate;
import tuanda.soict.firstsun.LogicPackage.Group;
import tuanda.soict.firstsun.LogicPackage.Scenario;

/**
 * @author tuanda_soict_hut
 * @version 0.0.1
 * @since 9-9-2012
 */
public class Pray_Ability extends Ability {

	public Pray_Ability(int id, String name, int range, int area, int affect) {
		super(id, name, range, area, affect);
	}

	@Override
	public void mainAct(Army army, Scenario scenario) {
		int level = getAbilityLevel();
		Coordinate current = army.getCoordinate();
		int range = this.getAbilityRange();
		Coordinate targetCoor = this.getTarget();
		BattleMap map = scenario.getBattleMap();
		Group[] group = scenario.getGroup();
		if (current.getDistance(targetCoor) <= range) {
			Army arm = map.getArmy(targetCoor.getLocationX(),
					targetCoor.getLocationY());
			if (arm != null) {
				if (group[arm.getGroupID()].getSide() == group[army
						.getGroupID()].getSide()) {
					Attack_Effect prayEffect = new Attack_Effect(this.getID(),
							this.getName(), 1, (level << 1) + 10
									+ (new Random()).nextInt(5), scenario);
					arm.addEffect(prayEffect);
					army.setExperience(army.getExperience() + (10 + level)
							* arm.getRank(), armyManager);
					army.setActionLeft(false);
				}
			}
		}
	}

	@Override
	public void calculation(Army army, Scenario scenario) {
		Coordinate targetCoor = this.getTarget();
		BattleMap map = scenario.getBattleMap();
		Group[] group = scenario.getGroup();
		Army arm = map.getArmy(targetCoor.getLocationX(),
				targetCoor.getLocationY());
		if (arm != null) {
			if (group[arm.getGroupID()].getSide() == group[army.getGroupID()]
					.getSide()) {
				this.setAbilityPoint(getAbilityLevel() * arm.getRank());
			}
		}
	}

	@Override
	public ArrayList<Coordinate> calculateAffectedArea(Army army,
			Scenario scenario) {
		ArrayList<Coordinate> temp = new ArrayList<Coordinate>();
		Coordinate target = this.getTarget();
		if (scenario.getBattleMap().isValidCoordinate(target)) {
			temp.add(target);
		}
		return temp;
	}
}
